* THIS DO-FILE GENERATES VARIABLES FOR MEDICAL PRESCRIPTIONS RELATED TO MENTAL HEALTH USING THE LMDB-DATA

global raw "Path to rawdata"

use panelraw_v7, clear
keep pnr year age birthyear
gen post=birthyear>1956
drop birthyear
sort pnr year
save tempmeds, replace

set more off
forvalues i = 1995/2015 {
use $raw/lmdb`i'.dta, clear
	keep pnr atc2 atc4 atc apk Volume
	gen med_lit=atc4=="N05AN"
	gen med_valpro=atc=="N03AG01"
	gen med_benzo=atc4=="N05BA" | atc4=="N05CF" | atc4=="N05CD"
	gen med_ssri=atc4=="N06AB" 
	gen med_adhd=atc=="N06BA01" | atc=="N06BA02" | atc=="N06BA04"
	gen med_ap2=atc=="N05AL05" | atc=="N05AX12" | atc=="N05AX16" | atc=="N05AX15" | atc=="N05AH02" | atc=="N05AE05" | atc=="N06AX11" | atc=="N05AH03" | atc=="N05AX13" | atc=="N05AH04" | atc=="N05AX08" | atc=="N05AE03" | atc=="N05AE04"
	gen med_n01=atc2=="N01"
	gen med_n02=atc2=="N02"
	gen med_n03=atc2=="N03" & med_valpro==0
	gen med_n04=atc2=="N04"
	gen med_n05=atc2=="N05" & med_lit==0
	gen med_n06=atc2=="N06" & med_ssri==0 & med_adhd==0
	gen med_n07=atc2=="N07"
	gen med_lit_dd=apk*Volume if med_lit==1
	gen med_valpro_dd=apk*Volume if med_valpro==1
	gen med_benzo_dd=apk*Volume if med_benzo==1
	gen med_ssri_dd=apk*Volume if med_ssri==1
	gen med_adhd_dd=apk*Volume if med_adhd==1
	gen med_lit_n=med_lit
	gen med_valpro_n=med_valpro
	gen med_benzo_n=med_benzo
	gen med_ssri_n=med_ssri
	gen med_adhd_n=med_adhd
	gen med_ap2_n=med_ap2
	collapse (max) med_lit-med_n07 (sum) med_lit_dd-med_adhd_dd med_lit_n-med_ap2_n, by(pnr)
	gen year=`i'
save temp`i', replace
}
use temp1995, clear
erase temp1995.dta
forvalues i = 1996/2014 {
	di `i'
	append using temp`i'.dta
	erase temp`i'.dta
	}
sort pnr year
label var med_lit "Lithium N05AN"
label var med_valpro "Valproic acid N03AG01"
label var med_benzo "Benzodiazepines N05BA CF CD"
label var med_adhd "ADHD-med N06BA01 03 04"
label var med_ap2 "2nd gen antipsychotics"
label var med_ssri "SSRI N06AB"
label var med_n01 "Anesthetic N01"
label var med_n02 "Painkillers N02"
label var med_n03 "Antiepileptics N03 (non-valpro)"
label var med_n04 "Anti-parkinson N04"
label var med_n05 "Psycholeptics N05 (non-lithium)"
label var med_n06 "Psychoanaleptics N06 (non-SSRI-ADHD)"
label var med_n07 "Other nervous system N07"
label var med_lit_dd "Daily doses, lithium N05AN"
label var med_valpro_dd "Daily doses, valproic acid N03AG01"
label var med_benzo_dd "Daily doses, benzodiazepines N05BA CF CD"
label var med_adhd_dd "Daily doses, ADHD-meds N06BA01 03 04"
label var med_ssri_dd "Daily doses, SSRI N06AB"
label var med_lit_n "Num of pre, lithium N05AN"
label var med_valpro_n "Num of pre, valproic acid N03AG01"
label var med_benzo_n "Num of pre, benzodiazepines N05BA CF CD"
label var med_adhd_n "Num of pre, ADHD-meds N06BA01 03 04"
label var med_ssri_n "Num of pre, SSRI N06AB"
compress
save medication_raw_v6, replace

use medication_raw_v6, clear
keep pnr year med_lit med_valpro med_benzo med_ssri med_lit_n med_ap2
merge 1:1 pnr year using tempmeds, nogen
mvencode med_*, mv(0) o
label var med_lit "Lithium"
label var med_valpro "Valpro"
label var med_benzo "Benzo"
label var med_ssri "SSRI"
label var med_ap2 "2nd gen antipsychotics"
	foreach x of varlist med_* {
		di "`: var label `x''"
		bys pnr (year): egen `x'_ev=max( `x' )
		label var `x'_ev "`: var label `x'' (ever)"
		gen age_`x'=age if `x'==1
		bys pnr (year): egen age_first_`x'=min(age_`x')
		gen year_to_`x'=age-age_first_`x'
		label var age_first_`x' "Age first diagnosis (`: var label `x'')" 
		label var year_to_`x' "Year to diagnosis (`: var label `x'')" 
		drop age_`x'
		label var `x' "`: var label `x'' (year)" 
	}
	foreach x of varlist med_* {
		gen `x'_post=post* `x'
		label var `x'_post "`: var label `x'' x post" 
	}
drop post age
order _all, alphabetic
order pnr year, first
order age_*, last
sort pnr year
save medication_v6, replace
erase tempmeds.dta
